//
//  FirstClassifyViewController.m
//  SDSY
//
//  Created by 蓝布鲁 on 2017/5/22.
//  Copyright © 2017年 蓝布鲁. All rights reserved.
//

#import "FirstClassifyViewController.h"
#import "NetworkTool.h"
#import "FirstClassModel.h"
#import "SecondClassifyViewController.h"

@interface FirstClassifyViewController ()<UITableViewDelegate, UITableViewDataSource>

// UI
@property (nonatomic,retain) UITableView *tableView;

// 数据
@property (nonatomic,retain) NSMutableArray *dataArray;

@end

@implementation FirstClassifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 获取分类数据
    [self getClassify];
    
    // 初始化tableView
    [self setupTableView];
    
    self.title = @"活动模块";
    
}

#pragma mark - UI设置
- (void)setupTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

#pragma mark - 网络请求
- (void)getClassify {
    [SVProgressHUD show];
    self.dataArray = [[NSMutableArray alloc] init];
    [[NetworkTool shareInstance] requireMethodType:POSTType
                                         URLString:@"SDSY/getActivityType"
                                        parameters:nil
                                           success:^(NSDictionary *respondDictionary) {
                                               [SVProgressHUD dismiss];
                                               for (NSDictionary *item in respondDictionary[@"type"]) {
                                                   FirstClassModel *model = [[FirstClassModel alloc] initWithDictionary:item];
                                                   [self.dataArray addObject:model];
                                               }
                                               [self.tableView reloadData];
                                           } failure:^(NSError *error) {
                                               [SVProgressHUD dismiss];
                                               
                                               [SVProgressHUD showfailed];
                                           }];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELLID"];
    FirstClassModel *model = self.dataArray[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    cell.textLabel.text = model.name;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    
    SecondClassifyViewController *controller = [[SecondClassifyViewController alloc] init];
    
    FirstClassModel *model = self.dataArray[indexPath.row];
    controller.tid = model.tid;
    [self.navigationController pushViewController:controller animated:true];
}


@end
